package com.example.service.impl;

import com.example.dao.DogWalkingDao;
import com.example.entity.DogWalking;
import com.example.service.DogWalkingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class DogWalkingServiceImpl implements DogWalkingService {
    @Autowired
    private DogWalkingDao dogWalkingDao;

    @Override
    public boolean createService(DogWalking service) {
        return dogWalkingDao.insertService(service) > 0;
    }

    @Override
    public List<DogWalking> getAvailableServices() {
        return dogWalkingDao.getAvailableServices();
    }

    @Override
    public List<DogWalking> getServicesByProvider(int providerId) {
        return dogWalkingDao.getServicesByProvider(providerId);
    }

    @Override
    public List<DogWalking> getServicesByRequester(int requesterId) {
        return dogWalkingDao.getServicesByRequester(requesterId);
    }

    @Override
    public boolean requestService(int serviceId, int requesterId) {
        return dogWalkingDao.updateServiceStatus(serviceId, requesterId, 1) > 0;
    }
    @Override
    public DogWalking getServiceById(int serviceId) {
        return dogWalkingDao.getServiceById(serviceId);
    }
    // 在获取服务详情时添加联系人电话
    @Override
    public DogWalking getServiceByIdWithContact(int serviceId) {
        DogWalking service = dogWalkingDao.getServiceById(serviceId);
        if (service != null) {
            String phone = dogWalkingDao.getContactPhoneByProviderId(service.getProvider_id());
            service.setUser_phone(phone);
        }
        return service;
    }
    @Override
    public DogWalking getServiceDetailsWithUserAndPet(int serviceId) {
        return dogWalkingDao.getServiceDetailsWithUserAndPet(serviceId);
    }

    @Override
    public boolean updateServiceStatus(Integer serviceId, Integer status) {
        try {
            // 查询当前服务状态（可选，用于状态校验）
            DogWalking service = dogWalkingDao.findServiceById(serviceId);
            if (service == null) {
                return false;
            }

            // 更新状态
            service.setStatus(status);
            int result = dogWalkingDao.updateService(service);
            return result > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}